package hw.txtreader.activity;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import hw.txtreader.DemoActivity;
import hw.txtreader.R;
import hw.txtreader.databinding.ActivityLoginBinding;
import hw.txtreader.utils.LogUtil;
import hw.txtreader.utils.SPUtil;

public class LoginActivity extends Activity {

    ActivityLoginBinding binding;
    String account,password;
    List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        binding.setActivity(this);
        initView();
    }

    private void initView() {

        Type listType = new TypeToken<List<String>>() {
        }.getType();
        String spRes = SPUtil.getString(LoginActivity.this,"module_list_user", "");
        LogUtil.e("spRes-->"+ spRes);
        if (spRes != null) {
            if (!TextUtils.isEmpty(spRes)) {
                Gson gson = new Gson();
                list = gson.fromJson(spRes, listType);
            }
        }

        binding.tvModuleLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                account = binding.etModuleAccount.getText().toString().trim();
                password = binding.etModulePassword.getText().toString().trim();
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(LoginActivity.this,"请输入您的账号",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this,"请输入您的密码",Toast.LENGTH_SHORT).show();
                    return;
                }

                for(String str : list){
                    LogUtil.e("账号密码分布-->"+ str);
                    if (str.contains(account) && str.contains(password)) {
                        SPUtil.putString(LoginActivity.this,"module_user_password",password);
                        SPUtil.putString(LoginActivity.this,"module_user_account",account);
                        startActivity(new Intent(LoginActivity.this, DemoActivity.class));
                        finish();
                    }
                }
            }
        });

        binding.tvModuleRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                finish();
            }
        });
    }
}
